package com.hundsun.yr.universal.p2p.ui.activity;

import android.annotation.SuppressLint;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.common.PackageUtils;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseMineWebActivity;
import com.hundsun.yr.universal.p2p.utils.KeyBoardListener;
import com.hundsun.yr.universal.p2p.utils.Logs;

import de.greenrobot.event.EventBus;


/**
 * @version V1.0
 * @Title: WebWithDrawActivity.java
 * @Description: 提现密码WEB页
 * @date 2016-4-8 下午3:30:53
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class WebWithDrawActivity extends BaseMineWebActivity implements OnClickListener {
    public HSSharedPreferences sharedPreferences = null;
    private boolean flag;

    /**
     * 目标跳转地址
     */
    private String flagUrl = "";
    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected View getLoadingTargetView() {

        if (mBrowserLayout == null) {
        }
        return mBrowserLayout;
    }


    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        mBrowserLayout.refresh();
        toggleHide();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
    }

    @Override
    protected void onNetworkDisConnected() {

        toggleNetworkError(true, new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (NetUtils.isNetworkConnected(getBaseContext())) {
                    showToast("请检查网络...");
                } else {
                    mBrowserLayout.refresh();
                    toggleHide();
                }
            }
        });

    }


    @SuppressLint("SetJavaScriptEnabled")
    @SuppressWarnings("deprecation")
    @Override
    protected void initViewsAndEvents() {
        super.initViewsAndEvents();
        toolbar_title.setText(mWebTitle);
        toolbar_menu.setText(R.string.complete);
        toolbar_menu.setTextColor(getResources().getColor(R.color.white));
        toolbar_menu.setVisibility(View.GONE);
        toolbar_menu.setOnClickListener(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        KeyBoardListener.getInstance(this).init();
        sharedPreferences = new HSSharedPreferences(mContext, Config.SPPath.file_userLoginedInfo);
        setCookie();
        WebChromeClient wvcc = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
//                Logs.e("ReceivedTitle: " + title);
                toolbar_title.setMaxLines(1);
                toolbar_title.setMaxEms(7);
                toolbar_title.setEllipsize(TextUtils.TruncateAt.END);
                if (title != null && !title.isEmpty()){
                    toolbar_title.setText(title);
                }
            }
        };
        mBrowserLayout.getWebView().setWebChromeClient(wvcc);

        mBrowserLayout.getWebView().getSettings().setJavaScriptEnabled(true);
        mBrowserLayout.getWebView().addJavascriptInterface(WebWithDrawActivity.this, "wjj");
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        // TODO Auto-generated method stub
        return false;
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.toolbar_menu:
                finish();
                break;
            default:
                break;
        }
    }
    @JavascriptInterface
    public void withdraw(){
        finish();
    }

    private void setCookie(){
        syncCookie(mContext,"",sharedPreferences.getShareProf(UserInfo.USER_NO),sharedPreferences.getShareProf(UserInfo.TOKENID));
    }


}
